package org.joinme.backend.service;

import org.joinme.backend.mapper.WechatUserMapper;
import org.joinme.backend.model.WechatUser;
import org.joinme.backend.util.ConstUtil;
import org.joinme.backend.util.DateUtil;
import org.joinme.backend.util.ErrorCodeMsg;
import org.joinme.backend.util.PrjException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class WechatUserService {
    @Autowired
    private WechatUserMapper wechatUserMapper;

    @Autowired
    private WechatService wechatService;

    public boolean userWechatLogin(HttpServletRequest request, String code, WechatUser userWechat, String appName) throws PrjException {
        String openId = wechatService.getOpenIdByCode(code, appName);

        int userId;
        //根据openId判断用户是否之前已经存在
        WechatUser userSaved = getUserWechatByOpenId(openId);
        //已存在，更新操作
        if (userSaved != null) {
            userWechat.setUserId(userSaved.getUserId());
            userWechat.setLastLogin(DateUtil.getCurrentTime());
            userWechat.setCreateTime(userSaved.getCreateTime());
            userWechat.setOpenId(openId);
            //额外设定一下，否则因为默认值为0 ，会被更新
            userWechat.setCredit(userSaved.getCredit());
            userWechat.setUserStatus(userSaved.getUserStatus());
            userWechat.setGender(userSaved.getGender());

            wechatUserMapper.updateByPrimaryKeySelective(userWechat);

            userId = userSaved.getUserId();
        } else {
            //第一次创建，新增
            userWechat.setOpenId(openId);
            userWechat.setCredit(ConstUtil.DEFAULT_CREDIT);
            userWechat.setUserStatus(ConstUtil.DEFAULT_USER_STATUS);

            addUserWechat(userWechat);

            userId = userWechat.getUserId();
        }

        request.getSession().setAttribute("userInfo", userId + "_" + openId);
        return true;
    }

    public boolean addUserWechat(WechatUser userWechat) {
        userWechat.setCreateTime(DateUtil.getCurrentTime());
        userWechat.setLastLogin(DateUtil.getCurrentTime());
        return wechatUserMapper.insertSelective(userWechat) > 0;
    }

    public WechatUser getUserWechatByOpenId(String wechatOpenId) {
        return wechatUserMapper.selectByOpenId(wechatOpenId);
    }

    public WechatUser getUserWechatById(int id) {
        return wechatUserMapper.selectByPrimaryKey(id);
    }

    //取得session中的userId
    public int getLoginUserId(HttpServletRequest request) throws PrjException {
        Object sUserInfo = request.getSession().getAttribute("userInfo");

        if(sUserInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_WECHAT_NOT_LOGIN);

        return Integer.parseInt(((String)sUserInfo).split("_")[0]);
    }

    //根据session中的userId，返回对应的user对象
    public WechatUser getLoginUserWechat(HttpServletRequest request) throws PrjException {
        Object sUserInfo = request.getSession().getAttribute("userInfo");

        if(sUserInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_WECHAT_NOT_LOGIN);

        return getUserWechatById(Integer.parseInt(((String) sUserInfo).split("_")[0]));
    }

    public boolean updateUserWechat(WechatUser userWechat) {
        WechatUser user = wechatUserMapper.selectByPrimaryKey(userWechat.getUserId());//获取原来信息
        //设定原有信息，防止被0覆盖
        if(userWechat.getUserStatus() == -9){
            userWechat.setUserStatus(user.getUserStatus());
        }
        if(userWechat.getGender() == -9){
            userWechat.setGender(user.getGender());
        }
        if(userWechat.getCredit() == -9){
            userWechat.setCredit(user.getCredit());
        }
        return wechatUserMapper.updateByPrimaryKeySelective(userWechat) > 0;
    }

    //获得特定的用户信息
    public WechatUser getSpecificWechatUser(int userId){
        return wechatUserMapper.selectByPrimaryKey(userId);
    }
}
